package communicationMessages;

import java.util.logging.Level;
import logger.LoggerWriter;
import database.DB;

public class ChangedBannedModeStateMessage extends Message {

	//chanedBannedModeStateMessage
		//userName operate
		//SubForum
		//userName to be banned
		//mode
	@Override
	public String getAnswer() {
		String answer = null;
		String []arr = this.content.split(delimiter);
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		if (arr.length != 4)
		{
			logger.log("failed"+"changedBannedModeStateMessage wrong format"+delimiter, Level.WARNING);
			return "failed"+delimiter+"changedBannedModeStateMessage wrong format"+delimiter;
		}
		else{
			answer = "ChangedBannedModeState" + delimiter;
			String operateUserName = arr[0];//operate
			String SubForum = arr[1];
			String userName = arr[2];
			String mode = arr[3];
			boolean state = Boolean.valueOf(mode); //if state is true -> banned user   else unbanned user
			int isActive = getMode(state);
			int userId = DB.getUserId(userName);
			
			if (state)
				{
				DB.changeStatetoBanned(userId,isActive);
				answer+=" User "+userName+ "is bannded by "+operateUserName+ " from subForum "+SubForum;
				}
				else 
					{
					DB.changeStatetoUNBanned(userId,isActive);
					answer+=" User "+userName+ "is UNbannded by "+operateUserName+ " from subForum "+SubForum;;
					}
		}
		return answer;
	}

			private int getMode(boolean mode) {
				return mode==true ? 1:0;
			}

}

